package com.example.mobile2021606127cxz0520homework;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private TextView tv;
    private String url = "http://192.168.1.242:3000/news";
    private OkHttpClient okHttpClient = new OkHttpClient();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = findViewById(R.id.tv_main_content);
        new Thread(() -> {
            Request request=new Request.Builder()
                    .url(url)
                    .build();
            Call call=okHttpClient.newCall(request);
            try {
                Response response=call.execute();
                String text=response.body().string();
               runOnUiThread(() -> tv.setText(text));
                Log.i("TAG",text);
            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }
}